<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- BIM获奖台账编辑 -->
    <a-form-model
      id="base-breadcrumb"
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
      style="position: relative"
    >
      <a-spin :spinning="loading">
        <!-- 基本信息 -->
        <a-card :bordered="false" style="margin-bottom: 50px">
          <title-name title="基本信息" />
          <custom-form
            :is-emit="false"
            :form-list="formList"
            :form="form"
            :edit-model="editModel == 0 ? 'V' : ''"
            :form-components="$refs"
          />
          <!-- 页脚操作栏 -->
          <div class="btn_box">
            <a-button
              v-if="editModel != 0"
              :loading="loading"
              type="primary"
              class="button_right"
              @click="handleSubmit"
            >
              提交
            </a-button>
            <a-button v-if="checkVisibleInDetail" type="primary" @click="handleActiveClose"> 关闭 </a-button>
          </div>
        </a-card>
      </a-spin>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import { insertBimAwardLedger, updateBimAwardLedger, getBimAwardLedgerById } from '@/api/bimIntegrate/bimResult.js'
import { getPageTypesLabel, getPageTreeLabel } from '@/api/bimIntegrate/bimResource.js'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import Selector from '@/components/Select/Selector'
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import SelectProject from '@/components/Select/SelectProject'

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  data() {
    return {
      loading: false, // 草稿和提交按钮loding
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      id: '', // 业务id
      form: {
        unitId: '', // 单位id
        unitName: '' // 单位
      }, // 表单
      rules: {
        projectId: [{ required: true, message: '请选择申报项目', trigger: 'change' }],
        declareTime: [{ required: true, message: '请选择申报时间', trigger: 'change' }],
        awardTime: [{ required: true, message: '请选择获奖时间', trigger: 'change' }],
        // awardLevelName: [{ required: true, message: '请选择奖项级别', trigger: 'change' }],
        gameNameId: [{ required: true, message: '请选择比赛名称', trigger: 'change' }],
        unitId: [{ required: true, message: '请选择单位', trigger: 'change' }],
        // organizers: [{ required: true, whitespace: true, message: '请输入举办单位', trigger: 'change' }],
        awardNameId: [{ required: true, message: '请选择奖项名称', trigger: 'change' }],
        completeUserId: [{ required: true, message: '请选择主要完成人', trigger: 'change' }],
        certificateAddressId: [{ required: true, whitespace: true, message: '请选择证书原件所在地', trigger: 'change' }]
        // fileInfoList: [{ type: 'array', required: true, message: '请上传文件', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.$route.meta.title = ['BIM获奖台账查看', 'BIM获奖台账编辑', 'BIM获奖台账新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading = true
      getBimAwardLedgerById({
        id: this.id
      })
        .then((res) => {
          this.loading = false
          this.form = res.data
        })
        .catch(() => {
          this.loading = false
        })
    } else {
      this.form.unitName = this.$currentPost()?.name // 默认单位
      this.form.unitId = this.$currentPost()?.id // 默认单位id
    }
  },
  computed: {
    // 表单列表
    formList() {
      return [
        {
          keyName: 'unitId',
          viewName: 'unitName',
          label: '单位',
          type: SelectCompanyAndProject,
          attrs: {
            isAutoSelectMe: true,
            extraParams: {
              notContainTags: ['PD']
            }
          },
          onSelectChange: (e) => {
            this.form.unitName = e?.name
            this.form.unitCode = e?.code
          }
        },
        {
          keyName: 'projectId',
          viewName: 'projectName',
          type: SelectProject,
          label: '申报项目',
          attrs: {
            keyValue: 'unitId',
            isEdit: this.editModel === 1,
            unitCode: this.form.unitCode
          },
          selectChange: (e) => {
            this.form.projectName = e?.name
          }
        },
        {
          keyName: 'declareTime',
          type: 'a-date-picker',
          label: '申报时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'completeUserId',
          type: 'SelectPerson',
          label: '主要完成人',
          isEcho: true,
          personKeys: ['completeUserId', 'completeUserName'],
          span: 24
        },
        {
          keyName: 'awardTime',
          type: 'a-date-picker',
          label: '获奖时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'awardNameId',
          viewName: 'awardName',
          type: Selector,
          label: '奖项名称',
          attrs: {
            customApi: getPageTypesLabel,
            customQuery: { param: { type: '30' } },
            dataValue: 'data.list'
          },
          getOption: (e) => {
            // 映射奖项名称name
            this.form.awardName = e?.name
          }
        },
        {
          keyName: 'gameNameId',
          viewName: 'gameName',
          type: Selector,
          label: '比赛名称',
          attrs: {
            customApi: getPageTreeLabel,
            customQuery: { needCount: false, query: { type: '1' } },
            dataValue: 'data.list'
          },
          getOption: (e) => {
            // 映射比赛名称name，以及举办单位和奖项级别（根据description识别是举办单位还好是奖项级别）
            this.$set(this.form, 'gameName', e?.name)
            this.$set(this.form, 'organizersId', e?.children.find((i) => i.description === '举办单位')?.id)
            this.$set(this.form, 'organizers', e?.children.find((i) => i.description === '举办单位')?.name)
            this.$set(this.form, 'awardLevel', e?.children.find((i) => i.description === '奖项级别')?.id)
            this.$set(this.form, 'awardLevelName', e?.children.find((i) => i.description === '奖项级别')?.name)
          }
        },
        {
          keyName: 'organizers',
          type: 'BaseInput',
          placeholder: '请先选择比赛名称',
          disabled: true,
          label: '举办单位'
        },
        {
          keyName: 'awardLevelName',
          type: 'BaseInput',
          placeholder: '请先选择比赛名称',
          disabled: true,
          label: '奖项级别'
        },
        {
          keyName: 'certificateAddressId',
          views: 'certificateAddress',
          label: '证书原件保管单位',
          type: SelectCompanyAndProject,
          attrs: {
            autoSelect: true,
            extraParams: {
              notContainTags: ['PD']
            }
          },
          onSelectChange: (e) => {
            this.form.certificateAddress = e?.name
          }
        },
        {
          keyName: 'fileInfoList',
          type: 'uploadFile',
          label: '附件',
          span: 24,
          businessCode: 'BIM02'
        }
      ]
    }
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit() {
      let that = this
      let url = [updateBimAwardLedger, insertBimAwardLedger][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        if (e) {
          // 提交表单
          that.loading = true
          let form = that.$clone(that.form)
          url(form)
            .then((res) => {
              that.loading = false
              if (res.code === '0' || res.data.code === '0') {
                that.$message.success('提交成功！')
                that.$multiTab.close()
              }
            })
            .catch((res) => {
              that.loading = false
            })
        }
      })
    }
  }
}
</script>